import { _decorator, Component, input, instantiate, Node, toDegree, UITransform, Vec3 } from 'cc';
import DataManager from '../../Global/DataManager';
import { EntityTypeEnum, IActor, InputTypeEnum, toFixed } from '../../Common';
import { EntityManager } from '../../Base/EntityManager';
import { EntityStateEnum, EventEnum } from '../../Enum';
import { WeaponStateMachine } from './WeaponStateMachine';
import EventManager from '../../Global/EventManager';
const { ccclass, property } = _decorator;

@ccclass('WeaponManager')
export class WeaponManager extends EntityManager {
    owner: number//所属角色id
    private body: Node;
    private anchor: Node;
    private point: Node;
    //初始化
    init(data: IActor) {
        this.owner = data.id;
        this.body = this.node.getChildByName("Body");
        this.anchor = this.body.getChildByName("Anchor");
        this.point = this.anchor.getChildByName("Point");
        if (!this.body || !this.anchor || !this.point) {
            console.error("没找到 Body 或Anchor 或Point");
        }

        this.fsm = this.body.addComponent(WeaponStateMachine);
        this.fsm.init(data.weaponType);

        this.state = EntityStateEnum.Idle;

        EventManager.Instance.on(EventEnum.WeaponShoot, this.handleWeaponShoot, this)
        EventManager.Instance.on(EventEnum.BulletBorn, this.handleBulletBorn, this)
    }
    protected onDisable(): void {
        EventManager.Instance.off(EventEnum.WeaponShoot, this.handleWeaponShoot, this);
        EventManager.Instance.off(EventEnum.BulletBorn, this.handleBulletBorn, this);
    }
    protected onEnable(): void {

    }
    protected onDestroy(): void {

    }
    handleWeaponShoot() {
        if (this.owner != DataManager.Instance.myPlayerId) {
          
            console.log("不是我的角色")
            console.log("this.owner"+this.owner+"DataManager.Instance.myPlayerId"+DataManager.Instance.myPlayerId)
            return;
        }//判断是否是我的角色
        const pointWorldPos = this.point.getWorldPosition();
        const pointStagePos = DataManager.Instance.stage.getComponent(UITransform).convertToNodeSpaceAR(pointWorldPos);
        const anchorWorldPos = this.anchor.getWorldPosition();

        const direction = new Vec3(pointWorldPos.x - anchorWorldPos.x, pointWorldPos.y - anchorWorldPos.y, 0);
        direction.normalize();
        console.log("发射子弹")

        EventManager.Instance.emit(EventEnum.ClientSyn, {
            type: InputTypeEnum.WeaponShoot,//输入类型
            owner: this.owner,
            position: {
                x: toFixed(pointStagePos.x),
                y: toFixed(pointStagePos.y),
                z: 0
            },
            direction: {
                x:toFixed(direction.x),
                y: toFixed(direction.y),
                z: 0
            },//输入方向
        })

        console.log(DataManager.Instance.state.bullet)
    }
    // 子弹生成 枪改变动画  
    handleBulletBorn(owner: number) {
        if (owner != this.owner) {
            return;
        }
        console.log("开枪枪动画")
        this.state = EntityStateEnum.Attack;
    }

}

